
<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="$store.state.headImgUrl"
            @click="editAvatarFn"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profile.name"
        @click="showNameDialogFn"
      />
      <!-- 修改用户名称的对话框 -->
      <van-dialog
        v-model="isShowNameDialog"
        title="修改名称"
        show-cancel-button
        :before-close="onNameDialogBeforeCloseFn"
      >
        <van-field
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入用户名"
          v-fofo
        />
      </van-dialog>

      <van-cell
      title="生日"
      is-link
      :value="profile.birthday"
      @click="showBirthFn"/>
      <!-- 修改用户生日的组件 -->
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择出生日期"
          :min-date="minDate"
          :max-date="maxDate"
          :show-toolbar='true'
          @confirm='confirmFn'
          @cancle='isShowBirth = false'
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { getProfileAPI, updateAvatarAPI, editProfileAPI } from '@/api'
import { mapMutations } from 'vuex'
import { Notify } from 'vant'
import dayjs from 'dayjs'
export default {
  name: 'UserEdit',
  data () {
    return {
      profile: {}, // 用户基本资料
      isShowNameDialog: false, // 是否显示姓名弹出框
      userName: '', // 修改的用户名
      isShowBirth: false, // 是否显示时间选择器
      minDate: new Date(1900, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期
    }
  },
  async created () {
    const res = await getProfileAPI()
    // console.log(res)
    this.profile = res.data.data
  },
  methods: {
    ...mapMutations(['setHeadImgUrlMutations']),
    // 确认修改生日
    async confirmFn () {
      // console.log(this.currentDate instanceof Date)
      const dateStr = dayjs(this.currentDate).format('YYYY-MM-DD')
      // console.log(dateStr)
      this.profile.birthday = dateStr
      this.profile.intro = '个人介绍'
      await editProfileAPI(this.profile)
      // console.log(res)
      // 时间选择器关闭
      this.isShowBirth = false
    },
    // 弹出时间选择框
    showBirthFn () {
      this.isShowBirth = true
      this.currentDate = new Date(this.profile.birthday)
    },
    // 姓名修改弹出层关闭前执行此方法
    async onNameDialogBeforeCloseFn (action, done) {
      if (action === 'confirm') {
        // console.log(action, done)
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          // 通过校验
          this.profile.name = this.userName
          await editProfileAPI(this.profile)
          done() // 关闭弹窗
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else {
        // 点击取消时 关闭弹窗
        done()
      }
    },
    // 点击编辑资料 弹出Dialog
    showNameDialogFn () {
      this.isShowNameDialog = true
      this.userName = this.profile.name
    },
    // 更换头像
    async onFileChange (e) {
      //   console.log(e.target.files[0])
      if (e.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('photo', e.target.files[0]) // photo在表单里参数名携带
      const res = await updateAvatarAPI(fd)
      //   console.log(res)
      // 上传成功, 更新前端数据头像地址
      this.profile.photo = res.data.data.photo
      this.setHeadImgUrlMutations(this.profile.photo)
    },
    editAvatarFn () {
      this.$refs.iptFile.click()
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
